package com.heima.MyFieldDemo;

import com.heima.MyflectDemo1.Student;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

public class MyFieldDemo1 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
        //获取字节码对象
        Class clazz = Class.forName("com.heima.MyflectDemo1.Student");
        //获得Constructor并创建学生类对象
        Constructor con = clazz.getConstructor();
        Student student = (Student) con.newInstance();
        System.out.println(student);
        //获取成员变量的对象
        Field name = clazz.getDeclaredField("name");
        name.setAccessible(true);
        name.set(student, "张三");
        Field age = clazz.getDeclaredField("age");
        age.setAccessible(true);
        age.set(student, 23);
        System.out.println(student);
    }
}
